package com.own.component.common.base.entity.vo;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.own.component.common.base.entity.po.BaseBean;
import com.own.constant.ConstantDateFormatPattern;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.NonNull;
import lombok.Setter;
import lombok.ToString;
import lombok.experimental.FieldNameConstants;
import org.springframework.format.annotation.DateTimeFormat;

import java.io.Serializable;
import java.time.LocalDateTime;

/**
 * BaseBeanVo
 *
 * @author chenxueli
 * @date 2022-09-10 12:12:00
 */
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
@FieldNameConstants
public class BaseBeanVo<T extends BaseBean> implements Serializable {

    @Schema(title = "主键id")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long id;

    @Schema(title = "创建时间")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonFormat(pattern = ConstantDateFormatPattern.Pattern.DATETIME, timezone = "GMT+8")
    @DateTimeFormat(pattern = ConstantDateFormatPattern.Pattern.DATETIME)
    private LocalDateTime gmtCreate;

    /**
     * 构造方法
     *
     * @param entity 原对象
     */
    public BaseBeanVo(@NonNull T entity) {
        this.id = entity.getId();
        this.gmtCreate = entity.getGmtCreate();
    }

    /**
     * 转po对象
     *
     * @return po对象
     */
    public final T toPo() {
        return toPo(null);
    }

    /**
     * 转po对象
     *
     * @param entity 原
     * @return po对象
     */
    public T toPo(T entity) {
        if (entity != null) {
            entity.setId(id);
            entity.setGmtCreate(gmtCreate);
        }
        return entity;
    }

    /**
     * 敏感词处理
     */
    public void desensitization() {
    }

    /**
     * 过滤敏感数据
     */
    public void filter() {
    }

}
